package com.edu.zju.scs.panacea.service.resource;

import com.edu.zju.scs.panacea.common.Global;
import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Dev;
import com.edu.zju.scs.panacea.entity.resource.Position;
import com.edu.zju.scs.panacea.mapper.resource.DevMapper;
import com.edu.zju.scs.panacea.mapper.resource.PositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-13 19:43
 */

@Service
public class DevService {

    @Autowired
    private DevMapper devMapper;

    @Autowired
    private PositionMapper positionMapper;

    public BaseResult getDev(Long id) {
        BaseResult baseResult = new BaseResult();
        Dev dev = devMapper.selectDev(id);
        Position position = positionMapper.selectPosition(id);
        if (dev == null) {
            baseResult.error("无此id对应的设备");
        } else {
            dev.setPosition(position);
            baseResult.success(dev);
        }
        return baseResult;
    }


    public BaseResult deleteDev(Long id) {
        BaseResult baseResult = new BaseResult();
        long deleteDevNums = devMapper.deleteDev(id);
        positionMapper.deletePosition(id);
        if (deleteDevNums == 0) {
            baseResult.error("删除设备失败！");
        } else {
            baseResult.success(deleteDevNums);
        }
        return baseResult;
    }

    public BaseResult addDev(Dev dev, Position position) {
        BaseResult baseResult = new BaseResult();
        // 返回插入记录条数
        Long insertNums = devMapper.insertDev(dev);
        position.setDevId(dev.getId());
        positionMapper.insertPosition(position);
        if (insertNums == null) {
            baseResult.error("新增设备失败！");
        } else {
            baseResult.success(insertNums);
        }
        return baseResult;
    }

    public BaseResult editDev(Dev dev) {
        BaseResult baseResult = new BaseResult();
        // 返回更新的记录条数
        Long updateNums = devMapper.updateDev(dev);
        positionMapper.updatePosition(dev.getPosition());
        if (updateNums == null) {
            baseResult.error("更新设备失败！");
        } else {
            baseResult.success(updateNums);
        }
        return baseResult;
    }


    public BaseResult getDevBySiteId(Long siteId, int type) {
        BaseResult baseResult = new BaseResult();
        List<Dev> devList = null;

        // 查找站点下的所有设备
        if (type == Global.ALL_DEV) {
            devList = devMapper.selectDevBySiteId(siteId);
        }

        // 查找站点下的已添加资源位的设备
        if (type == Global.RES_DEV) {
            devList = devMapper.selectResDevBySiteId(siteId);
        }

        // 查找站点下的未添加资源位的设备
        if (type == Global.NOT_RES_DEV) {
            devList = devMapper.selectNotResDevBySiteId(siteId);
        }

        for (Dev dev : devList) {
            Position position = positionMapper.selectPosition(dev.getId());
            dev.setPosition(position);
        }

        if (devList == null) {
            baseResult.error("查找设备失败！");
        } else {
            baseResult.success(devList);
        }
        return baseResult;
    }
}
